All Questions
Tagged with phpdependency-injection
41 questions
3votes
2answers
534views
Is it okay to use Dependency injection only because of unit testing?
I have a class that has dependencies that I know are not going to change. class ConversationFinder { public function __construct( protected Conversation $conversationDbFinder = new ...
4votes
1answer
881views
Cross-cutting concerns and dependency injection
I've got a web application with an architecture somewhat like a front-controller MVC design. I get HTTP requests, route them, run them through a filter layer, dispatch them to my domain classes which ...
1vote
3answers
146views
What considerations should I mind when designing methods or functions that take in a lot of parameters?
What considerations should I mind when designing methods or functions that take in a lot of parameters? A lot meaning over 4 but less than 10. Example, I am debating whether to pass in an array like ...
0votes
1answer
217views
Which types of objects that are instantiated inside controller's methods should be injected into the controller instead?
Consider code below MyController //MyAction //MyHandler { public function processRequest() { // ... $myObject = new MyObjectClass(); $myObject->methodCall(); ...
2votes
2answers
221views
What is wrong with writing web applications in a way that merges repository layer with view and with controller in the same file?
is there anything wrong with writing code in a way that merges view, repository, controller in the same file? i.e. global $db; // or $db = DBSingleton::get_db_instance(); // or global $container; $...
0votes
1answer
1kviews
Dependency Injection in one method to other method params
A teammate of mine wrote some code in the following way: Class Foo implements Job { // Framework uses type-hinting for dependency injection, only works on the handle() method // not on other methods ...
3votes
2answers
2kviews
Dependency Injection in Chain of Command pattern
I have a Chain of Command pattern implemented, with modules that implement an interface: interface IRequestHandler { public function handle(&$offset, &$tripData, $request = null); ...
-1votes
1answer
171views
Laravel Service Providers count and DI
I understand how service prividers in Laravel works and how to use them. But I'm not sure about how to keep clean code, specificaly in count of method parameters. For example I have this route: Route:...
2votes
1answer
259views
How to add extra functionality to an existing method class dynamically with DI?
I am looking for the best pattern to apply in my problem. I have an interface that defines my service class functionality interface NegotiationInterface { abstract public function ...
2votes
2answers
932views
How to use Dependency-injection Containers correctly when they hide dependencies from outer classes?
From Zend Docs there is this example of how to use Zend\Di, which is a dependency injection container of Zend Framework: // inside a bootstrap somewhere $di = new Zend\Di\Di(); // inside each ...
3votes
0answers
898views
Dependency injection - Nested objects
# Introduction I am working on a CMS application in PHP with about 200 classes. The CMS, in general, does the same thing every CMS does: generate sites. I am learning a lot about OOP and design ...
1vote
2answers
322views
Which programming technique can trim extra code generated by Dependency Injection?
Original Class class HomeController { function __construct() { $this->setPhpRenderer('Module'); $this->repository = new HomeRepository($id); $this->...
3votes
0answers
2kviews
Dependency Injection and class Inheritance
The project I am working on for about a year now was used to create a new mysqli connection to the database each time there was a Query to the database. In order to quickfix this problem at places ...
5votes
2answers
2kviews
PHP OOP dependency injection - when is it o.k. to use the "new" statement
I was told to avoid "new" statements in classes or functions but rather create the objects from classes in the root of the program (maybe with the use of a DI-container) and then inject the objects ...
5votes
2answers
2kviews
Dependency Injection & In-class Instantiation | Practical Limitations
Dependency Inversion is Good Inversion of dependency is good, it: Simplifies unit-testing Reduces coupling, allowing software components to be used interchangeably Keeps instantiation logic for a ...